home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
mac
/
source
/
music4c.sit
/
Music4C Folder
/
Sources Folder
/
PrepScore.c
< prev
next >
Wrap
Text File
|
1990-08-07
|
28KB
|
1,244 lines
/*
* ⌐ Graeme Gerrard 1990
* Faculty of Music, University of Melbourne
* Parkville Victoria 3052 Australia.
*
* ARPANET: grae@murdu.ucs.unimelb.edu.au
* telephone: (613) 344 4127, Fax: (613) 344 5346
*/
#include "Music4C.h"
#include <unix.h>
#include "ErrorAlert.h"
int read_section(void);
extern void Report(char *);
extern Event *warp_tempo(double *, Event *);
extern void renumber(Event *);
void add_end_ops(Event *);
void report2(Event *);
void fwrite2(Event *);
extern Boolean MAC_Check_Pass12_Events(int, int);
extern void PstringCopy(char *, char *);
extern void PstringCat(char *, char *);
Boolean getcard(void);
void parse_card(void);
void report1(int, char *, double *);
Event *insert_in_list(int, int, double *, Event *);
int decode(void);
int decode_t_card(int);
void set_output(void);
int straight_copy(void);
int straight_I_copy(void);
double myAtof(char *);
void check_event(Event *);
void write2_check(Event *);
void check_list(Event *);
FILE *p2out;
Str255 p2tmp;
int tempo_change;
static int lineno;
static int ncards;
static int n_i_cards;
static int sectno;
extern double srate;
static char card[MAXCARD];
static int op;
static int prevop;
static double instr_group;
static int previnstr_group;
static int nargs;
static double latest_time;
static double params[MAXPARAMS];
double *tparams[MAX_TEMPO_CARDS];
int totalcards;
int totalIcards;
int n_T_cards;
Event *list;
extern double TotalDuration;
extern int max_ins_types;
OSErr errnum;
static Boolean EcardRead;
char aString1[256];
char aString2[256];
extern Str255 theMess1, theMess2;
double atof();
void prepare_score(void);
extern void Pcheck_list(Event *);
Boolean FileFinished;
void prepare_score()
{
register int i;
extern int out1_flag, out2_flag;
extern int RunToPass1, RunToPass2, RunToPass3, CreateSoundFile;
long fTicks;
lineno = 1;
ncards = 0;
totalcards = 0;
totalIcards = 0;
TotalDuration = 0.0;
list = NIL;
sectno = 1;
max_ins_types = 0;
latest_time = 0.0;
EcardRead = FALSE;
if ( RunToPass2 || RunToPass3 || CreateSoundFile) { /* open pass2 out file */
Delay(0L, &fTicks);
NumToString(fTicks, &aString1);
PtoCstr((char *)aString1);
strcpy((char *)p2tmp, "-p2tmp.");
strcat((char *)p2tmp, aString1);
p2out = fopen((char *)p2tmp, "w+");
}
FileFinished = FALSE;
while ( !FileFinished ) {
tempo_change = 0;
n_T_cards = 0;
while ( ncards = read_section() ) {
if ( out1_flag ) {
sprintf((char *)theMess1, "End Pass1 of Section %d\n", sectno);
Report((char *)theMess1);
Report("--------------------------------------\n");
}
totalcards += ncards;
if ( out2_flag ) {
sprintf((char *)theMess1, "Pass2 output score for Section No. %d\n", sectno);
Report((char *)theMess1);
}
if ( RunToPass2 || RunToPass3 || CreateSoundFile) {
if ( tempo_change && ncards >= 2 ) {
for ( i = 0; i < n_T_cards; i++) {
list = warp_tempo(tparams[i], list);
}
}
if ( ncards >= 2 ) {
renumber(list);
}
add_end_ops(list);
if ( out2_flag )
report2(list);
fwrite2(list);
if ( out2_flag ) {
sprintf((char *)theMess1, "End Pass2 of Section %d\n", sectno);
Report((char *)theMess1);
Report("--------------------------------------\n");
}
MAC_Check_Pass12_Events(2, sectno);
}
list = NIL;
for ( i = 0; i < n_T_cards; i++)
DisposPtr((Ptr)tparams[i]);
n_T_cards = 0;
sectno++;
}
}
if (!EcardRead) {
PstringCopy((char *)theMess1, "\pNo end card in input file");
OSError(theMess1, NIL, NIL);
}
} /* prepare_score */
read_section()
{
int k;
Boolean NewSection;
extern int out1_flag;
extern int RunToPass1;
card[0] = '\0';
instr_group = 0.0;
previnstr_group = 0;
prevop = 0;
n_i_cards = 0;
ncards = 0;
NewSection = TRUE;
while ( (k = getcard())) {
if ( out1_flag && NewSection) {
NewSection = FALSE;
sprintf((char *)theMess1, "Pass1 output for Section %d\n", sectno);
Report((char *)theMess1);
}
ncards++;
parse_card();
if ( out1_flag )
report1(op, card, params);
if ( !RunToPass1 ) {
if ( op == FOP || op == IOP || op == SOP || op == EOP)
list = insert_in_list(op, nargs, params, list);
}
prevop = op;
if ( op == SOP || op == EOP )
break;
MAC_Check_Pass12_Events(1, sectno);
}
totalIcards += n_i_cards;
return(ncards);
}
Boolean getcard()
{
char buffer[1];
char *cp;
int ch;
OSErr theErr;
register int i;
extern int scoreRefNum;
long count;
card[0] = '\0';
i = 0;
cp = card;
count = 1L;
while( (theErr = FSRead(scoreRefNum, &count, &buffer)) == noErr) {
if ( *buffer == ';' )
break;
if (theErr == eofErr || count == 0L ) {
FileFinished = TRUE;
break;
}
*cp = buffer[0];
if ( *cp == ';')
break;
if ( isspace(*cp) ){
if ( *cp == '\n' || *cp == '\r' ) {
++lineno;
if ( i == 0 ) { /* start of card */
*cp = '\0';
}
else { /* in a card */
*cp++ = ' ';
i++;
}
}
else {
if ( i == 0 ) /* start of card */
*cp = '\0';
else {
cp++;
i++;
}
}
}
else { /* not a space */
cp++;
i++;
}
if ( i >= MAXCARD ) {
PstringCopy((char *)theMess1, "\pAn input line is too long, check for omitted record teminator ';'");
OSError(theMess1, NIL, NIL);
}
}
*cp = '\0';
if ( isupper(card[0]) )
card[0] += 0x20;
if ( card[0] == '\0' || theErr == eofErr || count == 0L ) {
FileFinished = TRUE;
return(FALSE);
}
else {
return(TRUE);
}
} /* getcard */
void parse_card()
{
register int i;
extern int nchnls;
extern double rescalingConst;
if (!isalpha(card[0])) {
sprintf((char *)theMess1, "Opcode error on line %d", lineno);
OSError(theMess1, NIL, NIL );
}
switch( card[0] ) {
case 'i':
case 'I':
op = IOP;
nargs = decode();
instr_group = params[0];
previnstr_group = (int)instr_group;
n_i_cards++;
if ( instr_group > max_ins_types )
max_ins_types = instr_group;
if ( latest_time < ( params[2] + params[3] ))
latest_time = ( params[2] + params[3] );
/*latest_time = MAX(latest_time, params[2] + params[3] );*/
break;
case 'f':
case 'F':
op = FOP;
nargs = decode();
previnstr_group = 0;
if ( latest_time < params[1] )
latest_time = params[1];
/*latest_time = MAX(latest_time, params[1] );*/
break;
case 's':
case 'S':
op = SOP;
params[0] = 0.0;
params[1] = 0.0;
nargs = decode();
previnstr_group = 0;
if ( nargs > 1 ) {
NumToString((long)sectno, aString2);
PstringCopy((char *)theMess2, "\pSection no: ");
PstringCat((char *)theMess2, (char *)aString2);
PstringCopy((char *)theMess1, "\pToo many arguments in section card");
OSError(theMess1, theMess2, NIL);
}
if ( params[0] <= 0.0 )
params[0] = latest_time;
else {
if ( params[0] < latest_time ) {
NumToString((long)sectno, aString2);
PstringCopy((char *)theMess2,"\pSection no: ");
PstringCat((char *)theMess2, (char *)aString2);
PstringCopy((char *)theMess1,"\pEnd time of section is greater than indicated duration");
OSError(theMess1, theMess2, NIL);
}
}
/* shift end time to p1 for easier comparisions in pass2 */
nargs++;
params[1] = params[0];
latest_time = 0.0;
break;
case 'e':
case 'E':
op = EOP;
EcardRead = TRUE;
params[0] = 0.0;
params[1] = 0.0;
nargs = decode();
previnstr_group = 0;
/* p0 = end time, p1 = nchnls, [p2 = sr] [p3 = rescaling constant ]*/
if ( nargs > 4 ) {
PstringCopy((char *)theMess1,"\pToo many arguments on 'e' record");
OSError(theMess1, NIL, NIL);
}
if ( params[1] > MAXCHANS || params[1] < 1.0 ) {
PstringCopy((char *)theMess1,"\pError in number of output channels on 'e' record");
PstringCopy((char *)theMess2,"\pMust be 1, 2 or 4");
OSError(theMess1, theMess2, NIL);
}
nchnls = (int) params[1];
if (params[2] <= 0.0 )
srate = SRATE;
else
srate = params[2];
rescalingConst = 0.0;
if ( nargs > 3 )
rescalingConst = params[3];
else
params[3] = 0.0;
if ( params[0] <= 0.0 )
params[0] = latest_time;
else {
if ( params[0] < latest_time ) {
NumToString((long)sectno, aString2);
PstringCopy((char *)theMess2,"\pSection no: ");
PstringCat((char *)theMess2, (char *)aString2);
PstringCopy((char *)theMess1,"\pEnd time of section is greater than indicated duration");
OSError(theMess1, theMess2, NIL);
}
latest_time = params[0];
}
/* shift end time to p1 for easier comparisions in pass2 */
nargs++;
params[4] = params[3];
params[3] = params[2];
params[2] = params[1];
params[1] = params[0];
params[0] = 0.0;
break;
case 't':
case 'T':
if ( n_T_cards >= MAX_TEMPO_CARDS) {
NumToString((long)sectno, aString2);
PstringCopy((char *)theMess2,"\pSection no: ");
PstringCat((char *)theMess2, (char *)aString2);
PstringCopy((char *)theMess1,"\pToo many tempo cards");
OSError(theMess1, theMess2, NIL);
}
op = TOP;
tempo_change = 1;
tparams[n_T_cards] = (double *)NewPtr(sizeof(double) * MAXPARAMS);
if ( (errnum = MemError()) != noErr ){
PstringCopy((char *)theMess1,"\pError allocating memory in parse_card for tempo_card");
OSError(theMess1, NIL, NIL);
}
/* zero fields of card */
for ( i = 0; i < MAXPARAMS; i++ )
*(tparams[n_T_cards]+i) = 0.0;
nargs = decode_t_card(n_T_cards);
n_T_cards++;
break;
case 'c':
case 'C':
case '!':
op = COP;
previnstr_group = 0;
break;
case 'o':
case 'O':
op = OOP;
nargs = decode();
previnstr_group = 0;
set_output();
break;
default:
NumToString((long)lineno, aString2);
PstringCopy(aString1, "\pon line ");
PstringCat(aString1, aString2);
PstringCat((char *)theMess2, (char *)aString1);
PstringCopy((char *)theMess1,"\pUnknown opcode");
OSError(theMess1, theMess2, NIL);
break;
}
} /* parse_card */
int decode()
{
register int i;
char *cp;
cp = card;
cp++;
if ( op != IOP ) {
return(straight_copy());
}
if ( n_i_cards < 1 || prevop != op) {
return(straight_I_copy());
}
/* check for same instr_group */
while (*cp && isspace(*cp))
cp++;
if ( *cp == CARRY_CODE ) {
instr_group = (double)previnstr_group;
}
else if ( isdigit(*cp) ) {
instr_group = atof(cp);
if ( (int)instr_group != previnstr_group) {
return(straight_I_copy());
}
}
else { /* error, not a number or a CARRY_CODE */
NumToString((long)lineno, aString2);
PstringCopy(aString1, "\pon line: ");
PstringCat(aString1, aString2);
PstringCat((char *)theMess2, (char *)aString1);
PstringCopy((char *)theMess1,"\pIllegal character in instrument number");
OSError(theMess1, theMess2, NIL);
}
/* we only get here if we may have to do carries */
params[0] = instr_group;
params[1] = 1.0; /* save for instrument number */
cp++;
while (*cp && !isspace(*cp))
cp++;
while (isspace(*cp))
cp++;
for ( i = 2; i < MAXPARAMS && *cp; i++) {
if (isdigit(*cp) || *cp == '.' || *cp == '-' || *cp == '+') {
params[i] = myAtof(cp);
}
else if ( *cp == ';' )
/* pass */
;
else if (*cp == '*') {
/* do nothing, old value in params */
}
while (*cp && !isspace(*cp)) /* skip rest of number */
cp++;
while (*cp && isspace(*cp)) /* skip to start of next number */
cp++;
if (!*cp) {
previnstr_group = (int)params[0];
return(nargs);
}
}
} /* decode */
straight_I_copy()
{
register int i;
char *cp;
/* params format for i cards is:
* params[0] = instr_group for i cards, fno for fcards.
* params[1] = (unused for i cards, later becomes instrument NUMBER, otherwise
(start time)
* params[2] = start time for i cards, gen no for f cards
* params[3] = duration for i cards
*/
cp = card;
nargs = 0;
cp++;
while (isspace(*cp))
cp++;
if (isdigit(*cp) || *cp == '.' || *cp == '-' || *cp == '+') {
params[0] = myAtof(cp);
nargs++;
cp++;
while (*cp && !isspace(*cp)) /* skip rest of number */
cp++;
while (*cp && isspace(*cp)) /* skip to start of next number */
cp++;
}
params[1] = 1.0;
nargs++;
while (isspace(*cp))
cp++;
for ( i = 2; i < MAXPARAMS && *cp; i++) {
if (isdigit(*cp) || *cp == '.' || *cp == '-' || *cp == '+') {
params[i] = myAtof(cp);
nargs++;
}
else if (*cp == CARRY_CODE) {
NumToString((long)lineno, aString2);
PstringCopy(aString1, "\pon line: ");
PstringCat(aString1, aString2);
PstringCat((char *)theMess2, (char *)aString1);
PstringCopy((char *)theMess1,"\pIllegal carry");
OSError(theMess1, theMess2, NIL);
}
else if ( *cp == ';' )
/* pass */
;
else {
NumToString((long)lineno, aString2);
PstringCopy(aString1, "\pon line: ");
PstringCat(aString1, aString2);
PstringCat((char *)theMess2, (char *)aString1);
PstringCopy((char *)theMess1,"\pIllegal character");
OSError(theMess1, theMess2, NIL);
}
while (*cp && !isspace(*cp)) /* skip rest of number */
cp++;
while (*cp && isspace(*cp)) /* skip to start of next number */
cp++;
if (!*cp) {
previnstr_group = (int)params[0];
return(nargs);
}
}
return(nargs);
} /* straight_I_copy */
int straight_copy()
{
register int i, k;
char *cp;
/* params format for i cards is:
* params[0] = instr_group for i cards, fno for fcards.
* params[1] = (unused for i cards, later becomes instrument NUMBER, otherwise
(start time)
* params[2] = start time for i cards, gen no for f cards
* params[3] = duration for i cards
*/
cp = card;
nargs = 0;
cp++;
while (isspace(*cp))
cp++;
for ( i = 0; i < MAXPARAMS && *cp; i++) {
if (isdigit(*cp) || *cp == '.' || *cp == '-' || *cp == '+') {
params[i] = myAtof(cp);
nargs++;
}
else if (*cp == CARRY_CODE) {
NumToString((long)lineno, aString2);
PstringCopy(aString1, "\pon line: ");
PstringCat(aString1, aString2);
PstringCat((char *)theMess2, (char *)aString1);
PstringCopy((char *)theMess1,"\pIllegal carry");
OSError(theMess1, theMess2, NIL);
}
else if ( *cp == ';' )
/* pass */
;
else {
NumToString((long)lineno, aString2);
PstringCopy(aString1, "\pon line: ");
PstringCat(aString1, aString2);
PstringCat((char *)theMess2, (char *)aString1);
PstringCopy((char *)theMess1,"\pIllegal character");
OSError(theMess1, theMess2, NIL);
}
while (*cp && !isspace(*cp)) /* skip rest of number */
cp++;
while (*cp && isspace(*cp)) /* skip to start of next number */
cp++;
if (!*cp) {
previnstr_group = (int)params[0];
return(nargs);
}
}
return(nargs);
} /* straight_copy */
int decode_t_card(n_T_cards)
int n_T_cards;
{
register int i;
char *cp = card;
nargs = 0;
cp++;
while (isspace(*cp))
cp++;
for ( i = 0; i < MAXPARAMS && *cp; i++) {
if (isdigit(*cp) || *cp == '.' || *cp == '-' || *cp == '+') {
*(tparams[n_T_cards]+i) = myAtof(cp);
nargs++;
}
else if ( *cp == ';' )
/* pass */
;
else {
sprintf((char *)aString1, "Section no %d line %d", sectno, lineno);
CtoPstr(aString1);
PstringCat((char *)theMess2, aString1);
PstringCat((char *)theMess1, "\pIllegal character in tempo record");
OSError(theMess1, theMess2, NIL);
}
while (*cp && !isspace(*cp)) /* skip rest of number */
cp++;
while (*cp && isspace(*cp)) /* skip to start of next number */
cp++;
if (!*cp) {
return(nargs);
}
}
} /* decode_t_card */
void report2(list)
Event *list;
{
/* prints event list on file for debugging purposes */
Event *cursor;
register int i;
register int n;
if ( list == NIL ) {
return;
}
cursor = list;
while ( cursor != NIL ) {
n = 0;
if ( cursor->op == ENDOP ) {
sprintf( (char *)theMess1, "%d", (int)cursor->op);
Report((char *)theMess1);
sprintf( (char *)theMess1, " %8.3f", *(cursor->pptr));
Report((char *)theMess1);
sprintf( (char *)theMess1, " %8.3f", *(cursor->pptr+1));
Report((char *)theMess1);
sprintf( (char *)theMess1, " %8.3f", *(cursor->pptr+2));
Report((char *)theMess1);
}
else {
sprintf( (char *)theMess1, " %d", cursor->op);
Report((char *)theMess1);
if ( cursor->op == EOP ) {
sprintf( (char *)theMess1, " %8.3f", *(cursor->pptr+1) );
Report((char *)theMess1);
sprintf( (char *)theMess1, " %8.1f", *(cursor->pptr+2) );
Report((char *)theMess1);
sprintf( (char *)theMess1, " %8.1f", *(cursor->pptr+3) );
Report((char *)theMess1);
}
else if ( cursor->op == SOP ) {
sprintf( (char *)theMess1, " %8.3f", *(cursor->pptr) );
Report((char *)theMess1);
}
else {
for ( i = 0; i < cursor->nargs; i++) {
n++;
sprintf((char *)theMess1, " %8.3f", *(cursor->pptr+i));
Report((char *)theMess1);
if ( n % 8 == 0 ) {
if ( i < cursor->nargs-1) {
Report("\n");
Report("\t");
}
}
}
}
}
Report("\n");
cursor = cursor->next; /* get next element */
}
} /* report2 */
void check_event(event)
Event *event;
{
/* prints event list on terminal for debugging purposes */
register int i;
register int n;
if ( !out1_flag)
return;
if ( event == NIL ) {
Report("null event\n");
return;
}
n = 0;
sprintf( (char *)theMess1, "%d", event->op);
Report((char *)theMess1);
sprintf( (char *)theMess1, " %d", event->nargs);
Report((char *)theMess1);
for ( i = 0; i< event->nargs; i++) {
n++;
sprintf( (char *)theMess1, " %8.3f", *(event->pptr+i));
Report((char *)theMess1);
if ( n % 8 == 0 ) {
if ( i < event->nargs-1) {
Report("\n");
Report("\t");
}
}
}
Report("\n");
} /* check_event */
void fwrite2(list)
Event *list;
{
register int i;
register int k;
Event *cursor, *c;
int lastop;
extern int RunToPass1;
int eventNo = 0;
int opcode;
cursor = list;
while ( cursor != NIL ) {
eventNo++;
lastop = cursor->op;
switch (cursor->op ) {
case SOP:
TotalDuration += *(cursor->pptr);
i = fwrite(&cursor->op, sizeof(int), 1, p2out);
i = fwrite(cursor->pptr, sizeof(double), 1, p2out);
break;
case EOP:
TotalDuration += *(cursor->pptr+1);
i = fwrite(&cursor->op, sizeof(int), 1, p2out);
i = fwrite(cursor->pptr, sizeof(double), 1, p2out);
break;
case ENDOP:
i = fwrite(&cursor->op, sizeof(int), 1, p2out);
i = fwrite(cursor->pptr, sizeof(double), 3, p2out);
break;
case IOP:
if ( *(cursor->pptr+3) <= 0.0) {
sprintf((char *)aString1, "Event %d has a duration of zero", eventNo);
CtoPstr((char *)aString1);
OSError(theMess1, NIL, NIL);
}
case FOP:
i = fwrite(&cursor->op, sizeof(int), 1, p2out);
i = fwrite(&cursor->nargs, sizeof(int), 1, p2out);
/* for ( i = 0; i< cursor->nargs; i++)*/
k = fwrite(cursor->pptr, sizeof(double), cursor->nargs, p2out);
break;
}
c = cursor;
cursor = cursor->next;
/* clean the list */
for ( i = 0; i < c->nargs; i++ ) {
DisposPtr((Ptr)c->pptr+i);
}
DisposPtr((Ptr)c);
}
/* if ( lastop == EOP ) {
fclose(p2out);
}
*/
} /* fwrite2 */
void write2_check(list)
Event *list;
{
register int i;
Event *cursor, *c;
int lastop;
if ( !out2_flag)
return;
Report("Output from write2_check\n");
cursor = list;
while ( cursor != NIL ) {
lastop = cursor->op;
switch (cursor->op ) {
case SOP:
case EOP:
sprintf( (char *)theMess1, "%d", cursor->op); Report((char *)theMess1);
sprintf( (char *)theMess1, " %f", *(cursor->pptr)); Report((char *)theMess1);
break;
case ENDOP:
sprintf( (char *)theMess1, " %d", cursor->op); Report((char *)theMess1);
sprintf( (char *)theMess1, " %lf", *(cursor->pptr)); Report((char *)theMess1);
sprintf( (char *)theMess1, " %lf", *(cursor->pptr+1)); Report((char *)theMess1);
sprintf( (char *)theMess1, " %lf", *(cursor->pptr+2)); Report((char *)theMess1);
break;
case IOP:
case FOP:
sprintf( (char *)theMess1, "%d", cursor->op); Report((char *)theMess1);
sprintf( (char *)theMess1, " %lf", *(cursor->pptr)); Report((char *)theMess1);
sprintf( (char *)theMess1, " %lf", *(cursor->pptr+1)); Report((char *)theMess1);
sprintf( (char *)theMess1, " %lf", *(cursor->pptr+2)); Report((char *)theMess1);
sprintf( (char *)theMess1, " %lf", *(cursor->pptr+3)); Report((char *)theMess1);
sprintf( (char *)theMess1, " nargs = %d", cursor->nargs ); Report((char *)theMess1);
break;
}
Report("\n");
Report("---------------------------\n");
cursor = cursor->next;
}
} /* write2_check */
void set_output()
{
extern out1_flag;
extern out2_flag;
extern out3_flag;
if ( nargs < 2 ) {
NumToString((long)lineno, aString2);
PstringCopy(aString1, "\pon line ");
PstringCat(aString1, aString2);
PstringCopy((char *)theMess2, aString1);
PstringCopy((char *)theMess1, "\pNot enough arguments on 'o' card");
OSError(theMess1, theMess2, NIL);
}
if ( (int)params[0] == 1 ) {
if ( (int)params[1] > 0 )
out1_flag = 1;
else
out1_flag = 0;
}
else if ( (int)params[0] == 2 ) {
if ( (int)params[1] > 0 )
out2_flag = 1;
else
out2_flag = 0;
}
else if ( (int)params[0] == 3 ) {
if ( (int)params[1] > 0 )
out3_flag = 1;
else
out3_flag = 0;
}
} /* set_output */
void report1(op, card, p)
int op;
char card[];
double p[];
{
register int i;
register int n;
char *cp = card;
if ( !out1_flag )
return;
if ( op == COP ) {
i = 0;
while ( *cp ) {
if ( i > MAXLINE && isspace(*cp)) {
Report( "\n");
cp++;
i = 0;
}
else {
/* Report(*cp++);*/
cp++;
i++;
}
}
Report(card);
Report(";\n");
return;
}
if ( op == OOP ) {
sprintf( (char *)theMess1, "%c\t%8.5f %8.5f;\n", card[0], p[0], p[1]);
Report((char *)theMess1);
return;
}
if ( op == SOP ) {
sprintf( (char *)theMess1, "%c\t%8.5f;\n", card[0], p[1]);
Report((char *)theMess1);
return;
}
if ( op == EOP ) {
sprintf( (char *)theMess1, "%c\t%8.5f %8.1f %8.1f", card[0], p[1], p[2], p[3]);
Report((char *)theMess1);
if ( p[4] > 0.0 ) {
sprintf( (char *)theMess1, " %8.5f;\n", p[4]);
Report((char *)theMess1);
}
else
Report(";\n");
return;
}
if ( op == TOP ) {
sprintf( (char *)theMess1, "%c\t", card[0]); Report((char *)theMess1);
n = 1;
for ( i = 0; i < nargs; i++) {
sprintf( (char *)theMess1, " %8.5f", *(tparams[n_T_cards-1]+i)); Report((char *)theMess1);
n++;
if ( n % 8 == 0 ) {
if ( i < nargs-1) {
Report("\n");
Report("\t ");
}
}
}
Report(";\n");
return;
}
sprintf( (char *)theMess1, "%c\t", card[0]); Report((char *)theMess1);
n = 1;
for ( i = 0; i < nargs; i++) {
if ( i != 1 ) { /* skip instrument number (not yet calculated anyway! */
sprintf( (char *)theMess1, " %8.5f", p[i]); Report((char *)theMess1);
n++;
if ( n % 8 == 0 ) {
if ( i < nargs-1) {
Report("\n");
Report("\t ");
}
}
}
}
Report(";\n");
} /* report1 */
Event *insert_in_list(op, nargs, a, b)
double a[];
int op;
int nargs;
Event *b;
{
/*
* Merge event a chronologically into list b.
*/
Event *head, *cursor, *prev, *temp;
int i;
int done = 0;
double s1, s2;
temp = (Event *)NewPtr(sizeof(Event));
if ( (errnum = MemError()) != noErr ){
PstringCopy((char *)theMess1,"\pError allocating memory in insert_in_list");
OSError(theMess1, NIL, NIL);
}
temp->nargs = nargs;
temp->op = op;
temp->next = NIL;
temp->pptr = (double *)NewPtr(temp->nargs * sizeof(double));
if ( (errnum = MemError()) != noErr ){
PstringCopy((char *)theMess1,"\pError allocating memory for parameters in insert_in_list");
OSError(theMess1, NIL, NIL);
}
for (i = 0; i<temp->nargs; i++) {
*(temp->pptr+i) = a[i];
/*sprintf( (char *)theMess1, "*(temp->pptr+i) = %f\n", *(temp->pptr+i));*/
}
head = b;
if (!head) {
head = temp;
head->next = NIL;
}
else {
done = 0;
prev = cursor = head;
while (cursor != NIL && !done ) {
/* this rats nest because i cards use different pnums for timing */
/* if ( temp->op == IOP || temp->op == EOP)*/
if ( temp->op == IOP)
s1 = *(temp->pptr+2);
else if ( temp->op == SOP )
s1 = *(temp->pptr);
else
s1 = *(temp->pptr+1);
if ( cursor->op == IOP )
s2 = *(cursor->pptr+2);
else if ( cursor->op == SOP )
s2 = *(cursor->pptr);
else
s2 = *(cursor->pptr+1);
if ( s1 < s2 ) {
if (cursor == head) {
/* insert before head */
temp->next = head;
head = temp;
done = 1;
}
else {
temp->next = prev->next;
prev->next = temp;
done = 1;
}
}
else
prev = cursor;
cursor = cursor->next;
}
if (!done) {
prev->next = temp;
temp->next = NIL;
}
}
return(head);
}
void check_list(list)
Event *list;
{
/* prints event list to reportfile for debugging purposes */
register int i;
register int n;
Event *cursor;
if ( !out2_flag)
return;
cursor = list;
while ( cursor != NIL ) {
n = 0;
sprintf( (char *)theMess1, "%d", cursor->op); Report((char *)theMess1);
sprintf( (char *)theMess1, " %d", cursor->nargs); Report((char *)theMess1);
for ( i = 0; i< cursor->nargs; i++) {
n++;
sprintf( (char *)theMess1, " %8.3f", *(cursor->pptr+i)); Report((char *)theMess1);
if ( n % 8 == 0 ) {
if ( i < cursor->nargs-1) {
Report("\n");
Report("\t");
}
}
}
Report("\n");
cursor = cursor->next;
}
Report("end of list\n");
} /* check_list */
void add_end_ops(list)
Event *list;
{
/* insert ENDOP cards in chronological order in list */
Event *cursor, *prev, *temp, *cnext;
int done;
double s1, s2;
cursor = list;
while ( cursor != NIL ) {
if ( cursor->op == IOP ) {
temp = (Event *)NewPtr(sizeof(Event));
if ( (errnum = MemError()) != noErr ){
PstringCopy((char *)theMess1,"\pError allocating memory in add_end_ops");
OSError(theMess1, NIL, NIL);
}
temp->nargs = 3;
temp->op = ENDOP;
temp->next = NIL;
temp->pptr = (double *)NewPtr(temp->nargs * sizeof(double));
if ( (errnum = MemError()) != noErr ){
PstringCopy((char *)theMess1,"\pError allocating memory for parameters in add_end_op");
OSError(theMess1, NIL, NIL);
}
*(temp->pptr) = *(cursor->pptr);
*(temp->pptr+1) = *(cursor->pptr+1);
*(temp->pptr+2) = *(cursor->pptr+2) + *(cursor->pptr+3);
done = 0;
prev = cursor;
cnext = cursor->next;
if ( temp->op == IOP || temp->op == ENDOP)
s1 = *(temp->pptr+2);
else
s1 = *(temp->pptr+1);
while (cnext != NIL && !done ) {
if ( cnext->op == IOP || cnext->op == ENDOP)
s2 = *(cnext->pptr+2);
else
s2 = *(cnext->pptr+1);
if ( cnext->op == SOP || cnext->op == EOP ) {
temp->next = prev->next;
prev->next = temp;
done = 1;
}
else if ( s1 <= s2 ) {
/* ensure functions preceed notes for same action time */
if ( cnext->op != FOP ) {
temp->next = prev->next;
prev->next = temp;
done = 1;
}
}
else
prev = cnext;
cnext = cnext->next;
}
if (!done) {
prev->next = temp;
}
}
cursor = cursor->next;
}
}
/*-------------------------------------------------------------------------------*/
double
myAtof(cp)
char *cp;
{
/* to overcome problem of atof() no converting leading zeros correctly */
register i;
double theDouble;
char *leadingZero;
Boolean eoNumber;
eoNumber = FALSE;
i = 0;
leadingZero = cp;
Start:
while (isspace(*leadingZero)) {
if ( !*leadingZero ) {
eoNumber = TRUE;
break;
}
leadingZero++;
}
if ( *leadingZero ) {
switch ( *leadingZero ) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
theDouble = atof(cp);
return(theDouble);
break;
case '+':
case '-':
leadingZero++;
goto Start;
case '.':
case '0':
break;
default:
break;
}
}
while (!eoNumber && *leadingZero != '.') {
if ( !*leadingZero ) {
eoNumber = TRUE;
break;
}
else if ( *leadingZero == ' ') {
eoNumber = TRUE;
break;
}
else {
leadingZero++;
i++;
}
switch ( *leadingZero ) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
eoNumber = TRUE;
break;
}
}
/* we have a '.', see if there are any 0s following, before a digit */
i = 0;
if ( !eoNumber ) {
leadingZero++;
while (*leadingZero == '0') {
leadingZero++;
i++;
}
}
theDouble = atof(cp);
while ( i > 0 ) {
theDouble /= 10.0;
i--;
}
return(theDouble);
}